﻿Imports System.Configuration


Public Class CLS_RECAUDACIONES
    Sub ProcesoGrat(ByRef rp As DotNetResponse.SQLPersistence)
        Try
            Dim exec As New DotNetDAL.SQLPersistence(True, "ConnectionString") ' Tablas propietarias

            Dim MiSRM2 'As New SRM
            With MiSRM2
                .Nodo = ConfigurationManager.AppSettings("Nodo")
                .Aplicacion = ConfigurationManager.AppSettings("App")
                .MasDatos = "N"
                .SiguienteRegistro = "0000001"
                .Mensaje = "seguros    SELECT * FROM tbl_parametros_comercial"
            End With

            CargaCuotas(MiSRM2, exec, rp)
            If rp.Errores Then
                rp.AddErrores(rp.MensajeError)
            End If

            rp.Errores = False
            exec.ExecuteNonQuery(rp, "sp_Cuotas_MONTO_CERO")
            If rp.Errores Then
                rp.AddErrores(rp.MensajeError)
            End If

            Dim rpget As New DotNetResponse.SQLPersistence

            Dim execget As New DotNetDAL.SQLPersistence(False, "ConnectionString") ' Tablas propietarias
            execget.ExecuteReader(rpget, "sp_Cuotas_Gratis")
            If rpget.Errores Then
                rp.AddErrores(rpget.MensajeError)
            End If

            Dim cantidad As Integer = rpget.GetDataReader.RecordsAffected
            Dim j As Integer
            If rpget.TieneDatos Then


                While rpget.GetDataReader.Read

                    j = j + 1

                    Dim Alerta As Boolean = False
                    Dim NroCMO As String = rpget.GetDataReader("MONTO_CUOTAMO")
                    Dim NroCPE As String = rpget.GetDataReader("MONTO_CUOTAPE")
                    Dim Est As String = rpget.GetDataReader("ESTADO")
                    Dim Poliza As String = rpget.GetDataReader("POLIZA")
                    Dim Nro_Seguro As String = rpget.GetDataReader("NRO_SEGURO")

                    Dim rpc As New DotNetResponse.SQLPersistence
                    'Dim FechaIn As String = rp.GetDataReader("Fecha_inicia_seguro")
                    'Dim FechaTer As String = rp.GetDataReader("Fecha_termino_seguro")

                    exec.ExecuteDataTable(rpc, "select Poliza, Fecha_Inicio, Fecha_Fin, Cuota_Param  from tbl_parametrocomercial where poliza='" & Poliza & "'")
                    If rpc.Errores Then
                        rp.AddErrores(rpc.MensajeError)
                    End If
                    Dim FechaINI As String = ""
                    Dim Fecha_TER As String = ""
                    Dim Cuota As String = ""

                    If rpc.TieneDatos Then
                        FechaINI = rpc.DtTable.Rows(0).Item("Fecha_Inicio")
                        Fecha_TER = rpc.DtTable.Rows(0).Item("Fecha_Fin")
                        Cuota = rpc.DtTable.Rows(0).Item("Cuota_Param")
                    End If

                    If rpc.TieneDatos = False Then 'Distinto de MO cero
                        Alerta = True
                    Else
                        If FechaINI <> "" And Fecha_TER <> "" Then
                            Alerta = True
                        Else
                            If Cuota <> "" Then
                                Alerta = True
                            End If
                        End If
                    End If


                    rpget.Errores = False
                    If Alerta = True Then
                        exec.ExecuteNonQuery(rpget, "UPDATE TBL_CUOTAS_VIG SET SRM = 'S', TIPOCUOTA ='CG' WHERE NRO_SEGURO ='" & Nro_Seguro & "'")
                    Else
                        exec.ExecuteNonQuery(rpget, "UPDATE TBL_CUOTAS_VIG SET SRM ='S', REVISADO = 'S', TIPOCUOTA ='CG' WHERE NRO_SEGURO ='" & Nro_Seguro & "'")
                    End If
                    If rpget.Errores Then
                        rp.AddErrores(rpget.MensajeError)
                    End If

                End While
            End If

            'Hacer el proceso del SRM
            'exec.StoreProcedure = " "
            'exec.ExecuteNonQuery(rp)
            exec.Commit()
            exec.Dispose()
            'rpget.GetDataReader.close()
            execget.Dispose()

        Catch ex As Exception
            rp.AddErrores(ex.Message)
        End Try


    End Sub

    Sub CargaCuotas(ByRef MiSRM2 As Object, ByRef exec As DotNetDAL.SQLPersistence, ByRef rp As DotNetResponse.SQLPersistence)
        Try
            MiSRM2.Invocar()

            Dim ej As String()
            Dim P(5) As String

            ej = Split(MiSRM2.Respuesta, "~")

            Dim i As Integer
            Dim j As Integer = 0

            For i = 0 To ej.Length - 1
                P(j) = ej(i)

                If j = 5 Then
                    rp.Errores = False
                    exec.ExecuteNonQuery(rp, "INSERT INTO  tbl_parametrocomercial (Estado_Param, Fecha_Inicio, Fecha_Fin, Cuota_Param, Poliza, Fecha_Filtro) values (" & P(0) & ",'" & Mid(P(1), 1, 10) & "','" & Mid(P(2), 1, 10) & "'," & P(3) & ",'" & P(4) & "'," & P(5) & ")")
                    If rp.Errores Then
                        rp.AddErrores(rp.MensajeError)
                    End If
                    j = -1
                End If
                j = j + 1
            Next


            If MiSRM2.MasDatos = "S" Then
                MiSRM2.Mensaje = "seguros    SELECT * FROM tbl_parametros_comercial"
                CargaCuotas(MiSRM2, exec, rp)
            End If

        Catch ex As Exception
            rp.AddErrores(ex.Message)
        End Try

    End Sub

End Class